#Block.py

import web
import json
from Gcode import Gcode

db = web.database(dbn='sqlite', db='/etc/debrew/debrew')

class Section:
    def __init__(self, attrs):
        self._attrs = attrs
        for a, b in attrs.items():
            setattr(self, a, b)

    def save(self):
        db.update('sections', where="id=$id", vars=self.attrs)

    @staticmethod
    def get_by_id(id):
        try:
            return Section(db.select('sections', where='id=$id', vars=locals())[0])
        except IndexError:
            return None

    @staticmethod
    def get_all_by_section_id(section_id):
        blocks = []
        for block in db.select('blocks', order="idx", where='section_id = $section_id', vars=locals()):
            blocks.append(Block(block))
        return blocks

   
    
